﻿using System;
using System.Collections.Generic;

namespace BaseThing
{
    public class CircularListNode<T>
    {
        public CircularListNode<T> previous, next;
        public T value;
    }

    public class CircularList<T>
    {
        public CircularListNode<T> first, last;

        public void Add(T value)
        {
            CircularListNode<T> node;

            node = new CircularListNode<T>();
            node.value = value;

            if (first == null)
            {
                first = node;
                last = node;
            }
            else
            {
                last.next = node;
                node.previous = last;

                last = node;
                first.previous = last;
                last.next = first;
            }
        }

        public void Remove(CircularListNode<T> node)
        {
            if (first == null)
            {
                return;
            }

            node.previous.next = node.next;
            node.next.previous = node.previous;

            if (node == first)
            {
                first = node.next;
            }

            if (node == last)
            {
                last = node.next;
            }
        }
    }
}
